package demo;

import com.tedu.InputUtil;

import java.util.List;
import java.util.Scanner;


public class Demo {
    public static void main(String[] args) {
        //用户注册
        Scanner sc = new Scanner(System.in);
        regUser();
//        sc.nextLine();
//        loginUser();
//        updateUser();
        //printAll();
    }
    public static void regUser(){
        User user = InputUtil.getInputObject(new User(),"欢迎注册");
        System.out.println(user);
        UserMapper userMapper = new UserMapper();
        if(userMapper.selectByUsername(user.getUsername())!=null){
            System.out.println("用户名已存在");
            return;
        }
        int num = userMapper.insert(user);
        System.out.println(num>0?"注册成功":"注册失败");
    }

    public static void loginUser(){
        UserLoginDTO dto= InputUtil.getInputObject(new UserLoginDTO(),"欢迎登录");
        System.out.println(dto);
        UserMapper userMapper = new UserMapper();
        User user = userMapper.selectByUsername(dto.getUsername());
        System.out.println(user);
        if(user==null){
            System.out.println("用户名不存在");
        }else if(!user.getPassword().equals(dto.getPassword())){
            System.out.println("密码错误");
        }else {
            System.out.println("登录成功,欢迎"+user.getUsername());
        }



    }

    public static void updateUser(){
        User user = InputUtil.getInputObject(new User(),"欢迎修改用户信息");
        UserMapper userMapper = new UserMapper();
        if(userMapper.update(user)>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    public static void printAll(){
        UserMapper userMapper = new UserMapper();
        List<User> list= userMapper.selectAll();
        if(list!=null){
            for (User user : list) {
                System.out.println(user);
            }
        }else {
            System.out.println("没有数据");
        }
    }
}
